package com.icexxx.parasol;

import java.util.Properties;

import org.springframework.core.env.Environment;

import io.swagger.annotations.Api;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.ApiSelectorBuilder;
import springfox.documentation.spring.web.plugins.Docket;

public class ConfigInformationInit {

    private static ConfigInformationProperties configInformation = null;

    public static ConfigInformationProperties data() {
        configInformation = new ConfigInformationProperties();
        configInformation.setName("API接口文档");
        configInformation.setTitle("API接口文档");
        configInformation.setDescription("API接口文档");
        configInformation.setVersion("1.0.0");
        configInformation.setEmail("");
        configInformation.setUrl("http://localhost:8080/");
        return configInformation;
    }

    public static Docket docket(ConfigInformationProperties configInformation, Environment environment) {
        String basePackage = configInformation.getBasePackage();
        String pathSelectors = configInformation.getPathSelectors();
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        ApiInfo apiInfo = apiInfo(configInformation);
        ApiSelectorBuilder apiSelectorBuilder = docket.apiInfo(apiInfo).select();
        if (basePackage != null && !"".equals(basePackage)) {
            apiSelectorBuilder = apiSelectorBuilder.apis(RequestHandlerSelectors.basePackage(basePackage));
        } else {
            apiSelectorBuilder = apiSelectorBuilder.apis(RequestHandlerSelectors.withClassAnnotation(Api.class));
        }
        if (pathSelectors != null && !"".equals(pathSelectors)) {
            if ("any".equalsIgnoreCase(pathSelectors)) {
                apiSelectorBuilder = apiSelectorBuilder.paths(PathSelectors.any());
            } else if ("none".equalsIgnoreCase(pathSelectors)) {
                apiSelectorBuilder = apiSelectorBuilder.paths(PathSelectors.none());
            } else if (pathSelectors.startsWith("regex:")) {
                String pathRegex = pathSelectors.substring("regex:".length());
                apiSelectorBuilder = apiSelectorBuilder.paths(PathSelectors.regex(pathRegex));
            } else if (pathSelectors.startsWith("ant:")) {
                String antPattern = pathSelectors.substring("ant:".length());
                apiSelectorBuilder = apiSelectorBuilder.paths(PathSelectors.ant(antPattern));
            }
        } else {
            boolean showApiDoc = false;
            if (environment != null) {
                String[] activeProfiles = environment.getActiveProfiles();
                showApiDoc = showApiDoc(activeProfiles);
            }
            if (showApiDoc) {
                apiSelectorBuilder = apiSelectorBuilder.paths(PathSelectors.any());
            } else {
                apiSelectorBuilder = apiSelectorBuilder.paths(PathSelectors.none());
            }
        }
        return apiSelectorBuilder.build();
    }

    private static ApiInfo apiInfo(ConfigInformationProperties configInformation) {
        String title = configInformation.getTitle();
        String description = configInformation.getDescription();
        String version = configInformation.getVersion();
        String name = configInformation.getName();
        String url = configInformation.getUrl();
        String email = configInformation.getEmail();
        String termsOfServiceUrl = configInformation.getTermsOfServiceUrl();
        Contact contact = null;
        if (name != null && !"".equals(name)) {
            if (url == null) {
                url = "";
            }
            if (email == null) {
                email = "";
            }
            contact = new Contact(name, url, email);
        }
        ApiInfoBuilder apiInfoBuilder = new ApiInfoBuilder();
        apiInfoBuilder = apiInfoBuilder.title(title).description(description).termsOfServiceUrl(termsOfServiceUrl).version(version);
        if (contact != null) {
            apiInfoBuilder.contact(contact);
        }
        return apiInfoBuilder.build();
    }

    private static boolean showApiDoc(String[] activeProfiles) {
        if (activeProfiles == null) {
            return true;
        }
        if (activeProfiles.length == 0) {
            Properties properties = System.getProperties();
            String value = (String) properties.get("os.name");
            if (value != null && value.toLowerCase().contains("windows")) {
                return true;
            }
            return false;
        }
        String defaultValue = activeProfiles[0];
        if ("prod".equalsIgnoreCase(defaultValue) || "uat".equalsIgnoreCase(defaultValue) || "product".equalsIgnoreCase(defaultValue)) {
            return false;
        }
        if ("dev".equalsIgnoreCase(defaultValue) || "test".equalsIgnoreCase(defaultValue) || "develop".equalsIgnoreCase(defaultValue)) {
            return true;
        }
        return false;
    }
}
